import React from 'react';
import { Spin } from 'antd';
import { isEqual, isEmpty } from 'lodash';
import {WeaErrorPage, WeaTools, WeaTable, WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const { ls } = WeaTools;
import Title from './Title';
import {setContentviewHeight} from "../../util/common";
//内容组件
class EContent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            tabdata: props.edata.data,
            currentTab: props.edata.currenttab,
            refresh: false,
        }

        this.handleTabData = this.handleTabData.bind(this);
    }

    componentWillReceiveProps(nextProps, nextState){
        if(!isEqual(this.props, nextProps)){
            this.setState({
                tabdata: nextProps.edata.data,
                currentTab: nextProps.edata.currenttab,
                refresh: false,
            });
        }
    }

    componentDidMount(){
        this.props.onRef(this);
    }

    shouldComponentUpdate(nextProps, nextState){
        return !isEqual(this.state, nextState) || !isEqual(this.props, nextProps)
    }
    handleTabData(tabid){
        const { config } = this.props;
        let { tabdata={} } = this.state;
        const { params, isremembertab } = config;
        const { currentTab } = this.state;
        if(!tabid) tabid = currentTab;
        tabdata = ls.getJSONObj('tabdata-' + params.eid + '-' + tabid+ '-' +config.userid) == null ? {} : ls.getJSONObj('tabdata-' + params.eid + '-' + tabid+ '-' +config.userid);
        this.setState({
            refresh: tabid !== currentTab,
            currentTab: tabid,
            tabdata,
        });
        if(isremembertab){
            let edata = ls.getJSONObj('edata-' + params.eid+ '-' +config.userid);
            if(edata){
                edata.currenttab = tabid;
                edata.data = tabdata;
                ls.set('edata-' + params.eid+ '-' +config.userid, edata);
            }
        }
        WeaTools.callApi('/api/portal/element/ecodeelementtab', 'POST', {...params, tabid}).then(result => {
            ls.set('tabdata-' + params.eid + '-' + tabid+ '-' +config.userid, result);
            if(tabid === this.state.currentTab){
                this.setState({
                    tabdata: result,
                    refresh: false,
                });
                if(isremembertab){
                    let edata = ls.getJSONObj('edata-' + params.eid+ '-' +config.userid);
                    if(edata){
                        edata.currenttab = tabid;
                        edata.data = result;
                        ls.set('edata-' + params.eid+ '-' +config.userid, edata);
                    }
                }
            }
        });
    }

    render() {
        const { config, edata, handleRefresh, handleDelete, fontSizeMp } = this.props;
        const { tabdata={}, currentTab, refresh } = this.state;
        const { tabsetting={} } = tabdata;
        const { tabids, titles, esetting } = edata;
        const { eid, ebaseid } = config.params;
        const { contentview = {},header } = config.item;
        let cstyle = {height: contentview.style.height,overflow: "auto"};
        if(header.displayPosition && header.displayPosition == 'bottom'){
            if(tabids && tabids.length > 1 || tabids && tabids.length == 1 && esetting.isAlwaysShow=='1'){
                if(contentview.style.height){
                    if(esetting.tabstyle == '1' && esetting.tabvalue){
                        cstyle = setContentviewHeight(contentview.style.height,esetting.tabvalue)
                    }else{
                        cstyle = {height: parseInt(contentview.style.height)-32+"px",overflow: "auto"};
                    }  
                }else {
                    cstyle = {height: contentview.style.height,overflow: "auto"};
                }
                let elementH=parseInt(contentview.style.height)-parseInt($(`#titleContainer_${eid}`).css('height'))-33;
                if(elementH!=parseInt(cstyle.height)){
                    if(elementH<parseInt(cstyle.height)){
                        cstyle.height=elementH+"px";
                    }
                }
            }
        }
        let contentHtml = <div/>;
        if(window.rewritePortalCusEle && typeof rewritePortalCusEle == 'function') {
            const props = {...this.props};
            const options = {
                config,
                fontSizeMp,
                isSetting: config.isSetting,
                id:eid,
                eid,
                ebaseid,
                params: config.params,
                boundID: tabdata.boundID,
            }
            contentHtml = window.rewritePortalCusEle(props, options);
        }
        const _titleProps = {
            currentTab,
            config,
            titles,
            tabids,
            handleRefresh,
            handleDelete,
            fontSizeMp,
            eMoreAddress: tabdata.moreUrl,
            eMoreShowType: '1',
            tabwidth: esetting.tabwidth,
            methodvalue: esetting.methodvalue,
            isAlwaysShow: esetting.isAlwaysShow,
            tabstyle: esetting.tabstyle,
            tabvalue: esetting.tabvalue,
            handleTabData: this.handleTabData
        }
        return <div>
            <Title ecId={`${this && this.props && this.props.ecId || ''}_Title@0u4ig6`} {..._titleProps}/>
            <div className="tabContant" id={`tabcontant_${eid}`} style={{height: cstyle.height,'overflow-y':'auto'}}>
                <div className="elementecodetab">
                    <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@10h3nw`} spinning={refresh}>{contentHtml}</Spin>
                </div>
            </div>
        </div>;
    }
}

class MyErrorHandler extends React.Component {
    render() {
        const hasErrorMsg = this.props.error && this.props.error !== "";
        return ( <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@68viaf`} msg = { hasErrorMsg ? this.props.error : getLabel(385218,"对不起，该页面异常，请联系管理员！")  }/>
        );
    }
}
EContent = WeaTools.tryCatch(React, MyErrorHandler, { error: "" })(EContent);
export default EContent;
